package com.pocket.insight.base.context;

import java.sql.Timestamp;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/5
 */
public class RelationToSqlContext {

    private static ThreadLocal<AtomicInteger> rewriteIndexThreadLocal = new ThreadLocal<>();
    private static ThreadLocal<Timestamp>     nowTimestampThreadLocal = new ThreadLocal<>();

    public static AtomicInteger getRewriteIndex() {
        if (rewriteIndexThreadLocal.get() == null)
            initializeShareItems();
        return rewriteIndexThreadLocal.get();
    }

    public static void setRewriteIndex(AtomicInteger index) {
        rewriteIndexThreadLocal.set(index);
    }

    public static void setNowTimestamp(long timeMillis) {
        nowTimestampThreadLocal.set(new Timestamp(timeMillis));
    }

    public static Timestamp getNowTimestamp() {
        return nowTimestampThreadLocal.get();
    }

    public static void initializeShareItems() {
        rewriteIndexThreadLocal.set(new AtomicInteger());
        nowTimestampThreadLocal.set(new Timestamp(System.currentTimeMillis()));
    }

    public static void remove() {
        rewriteIndexThreadLocal.remove();
        nowTimestampThreadLocal.remove();
    }

}
